﻿using System;
using System.Diagnostics;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using HslCommunication.Core;
using HslCommunication.Core.Net;
using HslCommunication.LogNet;
using HslCommunication.Reflection;

namespace HslCommunication.Robot.KUKA
{
	/// <summary>
	/// Kuka机器人的数据交互类，通讯支持的条件为KUKA 的 TCP通讯
	/// </summary>
	// Token: 0x0200002F RID: 47
	public class KukaTcpNet : NetworkDoubleBase, IRobotNet
	{
		/// <summary>
		/// 实例化一个默认的对象<br />
		/// Instantiate a default object
		/// </summary>
		// Token: 0x06000361 RID: 865 RVA: 0x0000DB57 File Offset: 0x0000BD57
		public KukaTcpNet()
		{
			base.ByteTransform = new ReverseWordTransform();
			this.LogMsgFormatBinary = false;
		}

		/// <summary>
		/// 实例化一个默认的Kuka机器人对象，并指定IP地址和端口号，端口号通常为9999<br />
		/// Instantiate a default Kuka robot object and specify the IP address and port number, usually 9999
		/// </summary>
		/// <param name="ipAddress">Ip地址</param>
		/// <param name="port">端口号</param>
		// Token: 0x06000362 RID: 866 RVA: 0x0000DB74 File Offset: 0x0000BD74
		public KukaTcpNet(string ipAddress, int port)
		{
			this.IpAddress = ipAddress;
			this.Port = port;
			base.ByteTransform = new ReverseWordTransform();
			this.LogMsgFormatBinary = false;
		}

		/// <inheritdoc />
		// Token: 0x06000363 RID: 867 RVA: 0x0000DBA4 File Offset: 0x0000BDA4
		public override OperateResult<byte[]> ReadFromCoreServer(Socket socket, byte[] send, bool hasResponseData = true, bool usePackHeader = true)
		{
			ILogNet logNet = base.LogNet;
			if (logNet != null)
			{
				logNet.WriteDebug(this.ToString(), StringResources.Language.Send + " : " + (this.LogMsgFormatBinary ? send.ToHexString(' ') : Encoding.ASCII.GetString(send)));
			}
			OperateResult operateResult = base.Send(socket, send);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				bool flag2 = this.receiveTimeOut < 0;
				if (flag2)
				{
					result = OperateResult.CreateSuccessResult<byte[]>(new byte[0]);
				}
				else
				{
					OperateResult<byte[]> operateResult2 = base.Receive(socket, -1, this.receiveTimeOut, null);
					bool flag3 = !operateResult2.IsSuccess;
					if (flag3)
					{
						result = operateResult2;
					}
					else
					{
						ILogNet logNet2 = base.LogNet;
						if (logNet2 != null)
						{
							logNet2.WriteDebug(this.ToString(), StringResources.Language.Receive + " : " + (this.LogMsgFormatBinary ? operateResult2.Content.ToHexString(' ') : Encoding.ASCII.GetString(operateResult2.Content)));
						}
						result = OperateResult.CreateSuccessResult<byte[]>(operateResult2.Content);
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000364 RID: 868 RVA: 0x0000DCC4 File Offset: 0x0000BEC4
		[DebuggerStepThrough]
		public override Task<OperateResult<byte[]>> ReadFromCoreServerAsync(Socket socket, byte[] send, bool hasResponseData = true, bool usePackHeader = true)
		{
			KukaTcpNet.<ReadFromCoreServerAsync>d__3 <ReadFromCoreServerAsync>d__ = new KukaTcpNet.<ReadFromCoreServerAsync>d__3();
			<ReadFromCoreServerAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadFromCoreServerAsync>d__.<>4__this = this;
			<ReadFromCoreServerAsync>d__.socket = socket;
			<ReadFromCoreServerAsync>d__.send = send;
			<ReadFromCoreServerAsync>d__.hasResponseData = hasResponseData;
			<ReadFromCoreServerAsync>d__.usePackHeader = usePackHeader;
			<ReadFromCoreServerAsync>d__.<>1__state = -1;
			<ReadFromCoreServerAsync>d__.<>t__builder.Start<KukaTcpNet.<ReadFromCoreServerAsync>d__3>(ref <ReadFromCoreServerAsync>d__);
			return <ReadFromCoreServerAsync>d__.<>t__builder.Task;
		}

		/// <summary>
		/// 读取Kuka机器人的数据内容，根据输入的变量名称来读取<br />
		/// Read the data content of the Kuka robot according to the input variable name
		/// </summary>
		/// <param name="address">地址数据</param>
		/// <returns>带有成功标识的byte[]数组</returns>
		// Token: 0x06000365 RID: 869 RVA: 0x0000DD25 File Offset: 0x0000BF25
		[HslMqttApi(ApiTopic = "ReadRobotByte", Description = "Read the data content of the Kuka robot according to the input variable name")]
		public OperateResult<byte[]> Read(string address)
		{
			return ByteTransformHelper.GetResultFromOther<byte[], byte[]>(base.ReadFromCoreServer(Encoding.UTF8.GetBytes(KukaTcpNet.BuildReadCommands(address))), new Func<byte[], OperateResult<byte[]>>(this.ExtractActualData));
		}

		/// <summary>
		/// 读取Kuka机器人的所有的数据信息，返回字符串信息，解码方式为UTF8，需要指定变量名称<br />
		/// Read all the data information of the Kuka robot, return the string information, decode by ANSI, need to specify the variable name
		/// </summary>
		/// <param name="address">地址信息</param>
		/// <returns>带有成功标识的字符串数据</returns>
		// Token: 0x06000366 RID: 870 RVA: 0x0000DD4E File Offset: 0x0000BF4E
		[HslMqttApi(ApiTopic = "ReadRobotString", Description = "Read all the data information of the Kuka robot, return the string information, decode by ANSI, need to specify the variable name")]
		public OperateResult<string> ReadString(string address)
		{
			return ByteTransformHelper.GetSuccessResultFromOther<string, byte[]>(this.Read(address), new Func<byte[], string>(Encoding.Default.GetString));
		}

		/// <summary>
		/// 根据Kuka机器人的变量名称，写入原始的数据内容<br />
		/// Write the original data content according to the variable name of the Kuka robot
		/// </summary>
		/// <param name="address">变量名称</param>
		/// <param name="value">原始的字节数据信息</param>
		/// <returns>是否成功的写入</returns>
		// Token: 0x06000367 RID: 871 RVA: 0x0000DD6D File Offset: 0x0000BF6D
		[HslMqttApi(ApiTopic = "WriteRobotByte", Description = "Write the original data content according to the variable name of the Kuka robot")]
		public OperateResult Write(string address, byte[] value)
		{
			return this.Write(address, Encoding.Default.GetString(value));
		}

		/// <summary>
		/// 根据Kuka机器人的变量名称，写入UTF8编码的字符串数据信息<br />
		/// Writes ansi-encoded string data information based on the variable name of the Kuka robot
		/// </summary>
		/// <param name="address">变量名称</param>
		/// <param name="value">ANSI编码的字符串</param>
		/// <returns>是否成功的写入</returns>
		// Token: 0x06000368 RID: 872 RVA: 0x0000DD81 File Offset: 0x0000BF81
		[HslMqttApi(ApiTopic = "WriteRobotString", Description = "Writes ansi-encoded string data information based on the variable name of the Kuka robot")]
		public OperateResult Write(string address, string value)
		{
			return this.Write(new string[]
			{
				address
			}, new string[]
			{
				value
			});
		}

		/// <summary>
		/// 根据Kuka机器人的变量名称，写入多个UTF8编码的字符串数据信息<br />
		/// Write multiple UTF8 encoded string data information according to the variable name of the Kuka robot
		/// </summary>
		/// <param name="address">变量名称</param>
		/// <param name="value">ANSI编码的字符串</param>
		/// <returns>是否成功的写入</returns>
		// Token: 0x06000369 RID: 873 RVA: 0x0000DD9D File Offset: 0x0000BF9D
		[HslMqttApi(ApiTopic = "WriteRobotStrings", Description = "Write multiple UTF8 encoded string data information according to the variable name of the Kuka robot")]
		public OperateResult Write(string[] address, string[] value)
		{
			return this.ReadCmd(KukaTcpNet.BuildWriteCommands(address, value));
		}

		// Token: 0x0600036A RID: 874 RVA: 0x0000DDAC File Offset: 0x0000BFAC
		private OperateResult ReadCmd(string cmd)
		{
			OperateResult<byte[]> operateResult = base.ReadFromCoreServer(Encoding.UTF8.GetBytes(cmd));
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				string @string = Encoding.UTF8.GetString(operateResult.Content);
				bool flag2 = @string.Contains("err");
				if (flag2)
				{
					result = new OperateResult("Result contains err: " + @string);
				}
				else
				{
					result = OperateResult.CreateSuccessResult();
				}
			}
			return result;
		}

		/// <summary>
		/// 启动机器人的指定的程序<br />
		/// Start the specified program of the robot
		/// </summary>
		/// <param name="program">程序的名字</param>
		/// <returns>是否启动成功</returns>
		// Token: 0x0600036B RID: 875 RVA: 0x0000DE1B File Offset: 0x0000C01B
		[HslMqttApi(Description = "Start the specified program of the robot")]
		public OperateResult StartProgram(string program)
		{
			return this.ReadCmd("03" + program);
		}

		/// <summary>
		/// 复位当前的程序<br />
		/// Reset current program
		/// </summary>
		/// <returns>复位结果</returns>
		// Token: 0x0600036C RID: 876 RVA: 0x0000DE2E File Offset: 0x0000C02E
		[HslMqttApi(Description = "Reset current program")]
		public OperateResult ResetProgram()
		{
			return this.ReadCmd("0601");
		}

		/// <summary>
		/// 停止当前的程序<br />
		/// Stop current program
		/// </summary>
		/// <returns>复位结果</returns>
		// Token: 0x0600036D RID: 877 RVA: 0x0000DE3B File Offset: 0x0000C03B
		[HslMqttApi(Description = "Stop current program")]
		public OperateResult StopProgram()
		{
			return this.ReadCmd("0621");
		}

		/// <inheritdoc cref="M:HslCommunication.Robot.KUKA.KukaTcpNet.Read(System.String)" />
		// Token: 0x0600036E RID: 878 RVA: 0x0000DE48 File Offset: 0x0000C048
		[DebuggerStepThrough]
		public Task<OperateResult<byte[]>> ReadAsync(string address)
		{
			KukaTcpNet.<ReadAsync>d__13 <ReadAsync>d__ = new KukaTcpNet.<ReadAsync>d__13();
			<ReadAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadAsync>d__.<>4__this = this;
			<ReadAsync>d__.address = address;
			<ReadAsync>d__.<>1__state = -1;
			<ReadAsync>d__.<>t__builder.Start<KukaTcpNet.<ReadAsync>d__13>(ref <ReadAsync>d__);
			return <ReadAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Robot.KUKA.KukaTcpNet.ReadString(System.String)" />
		// Token: 0x0600036F RID: 879 RVA: 0x0000DE94 File Offset: 0x0000C094
		[DebuggerStepThrough]
		public Task<OperateResult<string>> ReadStringAsync(string address)
		{
			KukaTcpNet.<ReadStringAsync>d__14 <ReadStringAsync>d__ = new KukaTcpNet.<ReadStringAsync>d__14();
			<ReadStringAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string>>.Create();
			<ReadStringAsync>d__.<>4__this = this;
			<ReadStringAsync>d__.address = address;
			<ReadStringAsync>d__.<>1__state = -1;
			<ReadStringAsync>d__.<>t__builder.Start<KukaTcpNet.<ReadStringAsync>d__14>(ref <ReadStringAsync>d__);
			return <ReadStringAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Robot.KUKA.KukaTcpNet.Write(System.String,System.Byte[])" />
		// Token: 0x06000370 RID: 880 RVA: 0x0000DEE0 File Offset: 0x0000C0E0
		[DebuggerStepThrough]
		public Task<OperateResult> WriteAsync(string address, byte[] value)
		{
			KukaTcpNet.<WriteAsync>d__15 <WriteAsync>d__ = new KukaTcpNet.<WriteAsync>d__15();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<KukaTcpNet.<WriteAsync>d__15>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Robot.KUKA.KukaTcpNet.Write(System.String,System.String)" />
		// Token: 0x06000371 RID: 881 RVA: 0x0000DF34 File Offset: 0x0000C134
		[DebuggerStepThrough]
		public Task<OperateResult> WriteAsync(string address, string value)
		{
			KukaTcpNet.<WriteAsync>d__16 <WriteAsync>d__ = new KukaTcpNet.<WriteAsync>d__16();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<KukaTcpNet.<WriteAsync>d__16>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Robot.KUKA.KukaTcpNet.Write(System.String[],System.String[])" />
		// Token: 0x06000372 RID: 882 RVA: 0x0000DF88 File Offset: 0x0000C188
		[DebuggerStepThrough]
		public Task<OperateResult> WriteAsync(string[] address, string[] value)
		{
			KukaTcpNet.<WriteAsync>d__17 <WriteAsync>d__ = new KukaTcpNet.<WriteAsync>d__17();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<KukaTcpNet.<WriteAsync>d__17>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		// Token: 0x06000373 RID: 883 RVA: 0x0000DFDC File Offset: 0x0000C1DC
		[DebuggerStepThrough]
		private Task<OperateResult> ReadCmdAsync(string cmd)
		{
			KukaTcpNet.<ReadCmdAsync>d__18 <ReadCmdAsync>d__ = new KukaTcpNet.<ReadCmdAsync>d__18();
			<ReadCmdAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<ReadCmdAsync>d__.<>4__this = this;
			<ReadCmdAsync>d__.cmd = cmd;
			<ReadCmdAsync>d__.<>1__state = -1;
			<ReadCmdAsync>d__.<>t__builder.Start<KukaTcpNet.<ReadCmdAsync>d__18>(ref <ReadCmdAsync>d__);
			return <ReadCmdAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Robot.KUKA.KukaTcpNet.StartProgram(System.String)" />
		// Token: 0x06000374 RID: 884 RVA: 0x0000E028 File Offset: 0x0000C228
		[DebuggerStepThrough]
		public Task<OperateResult> StartProgramAsync(string program)
		{
			KukaTcpNet.<StartProgramAsync>d__19 <StartProgramAsync>d__ = new KukaTcpNet.<StartProgramAsync>d__19();
			<StartProgramAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<StartProgramAsync>d__.<>4__this = this;
			<StartProgramAsync>d__.program = program;
			<StartProgramAsync>d__.<>1__state = -1;
			<StartProgramAsync>d__.<>t__builder.Start<KukaTcpNet.<StartProgramAsync>d__19>(ref <StartProgramAsync>d__);
			return <StartProgramAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Robot.KUKA.KukaTcpNet.ResetProgram" />
		// Token: 0x06000375 RID: 885 RVA: 0x0000E074 File Offset: 0x0000C274
		[DebuggerStepThrough]
		public Task<OperateResult> ResetProgramAsync()
		{
			KukaTcpNet.<ResetProgramAsync>d__20 <ResetProgramAsync>d__ = new KukaTcpNet.<ResetProgramAsync>d__20();
			<ResetProgramAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<ResetProgramAsync>d__.<>4__this = this;
			<ResetProgramAsync>d__.<>1__state = -1;
			<ResetProgramAsync>d__.<>t__builder.Start<KukaTcpNet.<ResetProgramAsync>d__20>(ref <ResetProgramAsync>d__);
			return <ResetProgramAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Robot.KUKA.KukaTcpNet.StopProgram" />
		// Token: 0x06000376 RID: 886 RVA: 0x0000E0B8 File Offset: 0x0000C2B8
		[DebuggerStepThrough]
		public Task<OperateResult> StopProgramAsync()
		{
			KukaTcpNet.<StopProgramAsync>d__21 <StopProgramAsync>d__ = new KukaTcpNet.<StopProgramAsync>d__21();
			<StopProgramAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<StopProgramAsync>d__.<>4__this = this;
			<StopProgramAsync>d__.<>1__state = -1;
			<StopProgramAsync>d__.<>t__builder.Start<KukaTcpNet.<StopProgramAsync>d__21>(ref <StopProgramAsync>d__);
			return <StopProgramAsync>d__.<>t__builder.Task;
		}

		// Token: 0x06000377 RID: 887 RVA: 0x0000E0FC File Offset: 0x0000C2FC
		private OperateResult<byte[]> ExtractActualData(byte[] response)
		{
			return OperateResult.CreateSuccessResult<byte[]>(response);
		}

		/// <inheritdoc />
		// Token: 0x06000378 RID: 888 RVA: 0x0000E114 File Offset: 0x0000C314
		public override string ToString()
		{
			return string.Format("KukaTcpNet[{0}:{1}]", this.IpAddress, this.Port);
		}

		/// <summary>
		/// 构建读取变量的报文命令
		/// </summary>
		/// <param name="address">地址信息</param>
		/// <returns>报文内容</returns>
		// Token: 0x06000379 RID: 889 RVA: 0x0000E134 File Offset: 0x0000C334
		public static string BuildReadCommands(string[] address)
		{
			bool flag = address == null;
			string result;
			if (flag)
			{
				result = string.Empty;
			}
			else
			{
				StringBuilder stringBuilder = new StringBuilder("00");
				for (int i = 0; i < address.Length; i++)
				{
					stringBuilder.Append(address[i] ?? "");
					bool flag2 = i != address.Length - 1;
					if (flag2)
					{
						stringBuilder.Append(",");
					}
				}
				result = stringBuilder.ToString();
			}
			return result;
		}

		/// <summary>
		/// 构建读取变量的报文命令
		/// </summary>
		/// <param name="address">地址信息</param>
		/// <returns>报文内容</returns>
		// Token: 0x0600037A RID: 890 RVA: 0x0000E1B0 File Offset: 0x0000C3B0
		public static string BuildReadCommands(string address)
		{
			return KukaTcpNet.BuildReadCommands(new string[]
			{
				address
			});
		}

		/// <summary>
		/// 构建写入变量的报文命令
		/// </summary>
		/// <param name="address">地址信息</param>
		/// <param name="values">数据信息</param>
		/// <returns>字符串信息</returns>
		// Token: 0x0600037B RID: 891 RVA: 0x0000E1D4 File Offset: 0x0000C3D4
		public static string BuildWriteCommands(string[] address, string[] values)
		{
			bool flag = address == null || values == null;
			string result;
			if (flag)
			{
				result = string.Empty;
			}
			else
			{
				bool flag2 = address.Length != values.Length;
				if (flag2)
				{
					throw new Exception(StringResources.Language.TwoParametersLengthIsNotSame);
				}
				StringBuilder stringBuilder = new StringBuilder("01");
				for (int i = 0; i < address.Length; i++)
				{
					stringBuilder.Append(address[i] + "=");
					stringBuilder.Append(values[i] ?? "");
					bool flag3 = i != address.Length - 1;
					if (flag3)
					{
						stringBuilder.Append(",");
					}
				}
				result = stringBuilder.ToString();
			}
			return result;
		}

		/// <summary>
		/// 构建写入变量的报文命令
		/// </summary>
		/// <param name="address">地址信息</param>
		/// <param name="value">数据信息</param>
		/// <returns>字符串信息</returns>
		// Token: 0x0600037C RID: 892 RVA: 0x0000E290 File Offset: 0x0000C490
		public static string BuildWriteCommands(string address, string value)
		{
			return KukaTcpNet.BuildWriteCommands(new string[]
			{
				address
			}, new string[]
			{
				value
			});
		}
	}
}
